
package com.insigma.business.qrcode.welfare.service.impl;


import com.insigma.business.qrcode.common.service.SequenceGenerator;
import com.insigma.business.qrcode.common.util.AdminAreaCodeUtils;
import com.insigma.business.qrcode.soldier.dto.SequenceNameConstant;
import com.insigma.business.qrcode.soldier.entity.DataStatsCDO;
import com.insigma.business.qrcode.welfare.dao.ScanCodeRecordDao;
import com.insigma.business.qrcode.welfare.dto.ScanCodeRecordBSearcher;
import com.insigma.business.qrcode.welfare.entity.ScanCodeRecordDO;
import com.insigma.business.qrcode.welfare.service.ScanCodeRecordService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


@Service("scanCodeRecordService")
public class ScanCodeRecordServiceImpl implements ScanCodeRecordService {

    @Autowired(required = false)
    private ScanCodeRecordDao scanCodeRecordDao;
    @Autowired(required = false)
    private SequenceGenerator sequenceGenerator;

    
    @Override
    public List<ScanCodeRecordDO> search(final ScanCodeRecordBSearcher scanCodeRecordBSearcher) {
        return scanCodeRecordDao.search(scanCodeRecordBSearcher);
    }
   
    @Override
    public ScanCodeRecordDO get(final String id){
        return scanCodeRecordDao.get(id);
    }

    
    @Override
    public Integer insert(final ScanCodeRecordDO scanCodeRecordDO){
        scanCodeRecordDO.setId(sequenceGenerator.genKey(SequenceNameConstant.SCAN_CODE_RECORD_ID));
        return scanCodeRecordDao.insert(scanCodeRecordDO);
    }

    
    @Override
    public Integer update(final ScanCodeRecordDO scanCodeRecordDO){
        return scanCodeRecordDao.update(scanCodeRecordDO);
    }

    
    @Override
    public Integer delete(final String id, final String modifierType, final String modifierId){
        return scanCodeRecordDao.delete(id,modifierType,modifierId);
    }

    @Override
    public List<ScanCodeRecordDO> searchMerchantScanRecord(ScanCodeRecordBSearcher searcher) {
        return scanCodeRecordDao.searchMerchantScanRecord(searcher);
    }

    @Override
    public List<ScanCodeRecordDO> searchMoneyNotNull(ScanCodeRecordBSearcher innerSearcher) {
        if (Strings.isNotBlank(innerSearcher.getAdminAreaCode())) {
            innerSearcher.setAdminAreaCode(AdminAreaCodeUtils.getCutdAreaCode(innerSearcher.getAdminAreaCode()));
        }
        return scanCodeRecordDao.searchMoneyNotNull(innerSearcher);
    }

    @Override
    public int countMerchantCountRecord(ScanCodeRecordBSearcher searcher) {
        return scanCodeRecordDao.countMerchantCountRecord(searcher);
    }

    @Override
    public Integer countKsme() {
        return scanCodeRecordDao.countKsme();
    }

    @Override
    public Integer countKsme(String adminAreaCode) {
        return scanCodeRecordDao.countKsmeByArea(AdminAreaCodeUtils.getCutdAreaCode(adminAreaCode));
    }

    @Override
    public Integer countCjxfScan() {
        return scanCodeRecordDao.countCjxfScan();
    }

    @Override
    public Integer countCjxfScan(String adminAreaCode) {
        adminAreaCode = AdminAreaCodeUtils.getCutdAreaCode(adminAreaCode);
        int i = scanCodeRecordDao.countCjxfScanEqSoldierId(adminAreaCode);
        int j = scanCodeRecordDao.countCjxfScanEqIdCardNo(adminAreaCode);
        return i + j;
    }

    @Override
    public Double countKsmeMoney() {
        Double integer = scanCodeRecordDao.countKsmeMoney();
        if (integer == null) {
            return 0.;
        }
        return integer;
    }

    @Override
    public Double countKsmeMoney(String adminAreaCode) {
        Double aDouble = scanCodeRecordDao.countKsmeMoneyByArea(AdminAreaCodeUtils.getCutdAreaCode(adminAreaCode));
        if (aDouble == null) {
            aDouble = 0.;
        }
        return aDouble;
    }

    @Override
    public List<DataStatsCDO> countCjxfScanAreaMap(String adminAreaCode) {
        int level = AdminAreaCodeUtils.getLevel(adminAreaCode);
        String areaLike = AdminAreaCodeUtils.getCutdAreaCode(adminAreaCode) + "%";
        int nextLevel = level + 1;
        int levelLength = AdminAreaCodeUtils.getLevelLength(nextLevel);
        return scanCodeRecordDao.countCjxfScanAreaMap(areaLike, nextLevel, levelLength);
    }

    @Override
    public List<DataStatsCDO> countKsmeScanAreaMap(String adminAreaCode) {
        int level = AdminAreaCodeUtils.getLevel(adminAreaCode);
        String areaLike = AdminAreaCodeUtils.getCutdAreaCode(adminAreaCode) + "%";
        int nextLevel = level + 1;
        int levelLength = AdminAreaCodeUtils.getLevelLength(nextLevel);
        return scanCodeRecordDao.countKsmeScanAreaMap(areaLike, nextLevel, levelLength);
    }

    @Override
    public Integer countCjxfScanBetweenTime(String adminAreaCode, Date morning, Date timesNight) {
        return scanCodeRecordDao.countCjxfScanBetweenTime(AdminAreaCodeUtils.getCutdAreaCode(adminAreaCode) + "%", morning, timesNight);
    }

    @Override
    public Integer countByBizName(String name) {
        return scanCodeRecordDao.countByBizName(name);
    }

    @Override
    public Integer countByBizNameOfHospital(String adminAreaCode, String bizName) {
        return scanCodeRecordDao.countByAreaAndBizNameOfHospital(AdminAreaCodeUtils.getCutdAreaCode(adminAreaCode), bizName);
    }

    @Override
    public Integer countByBizNameAndDate(String name, Date startDate, Date endDate) {
        return  scanCodeRecordDao.countByBizNameAndDate(name, startDate, endDate);
    }

    @Override
    public List<ScanCodeRecordDO> searchHospitalRecord(ScanCodeRecordBSearcher scanCodeRecordBSearcher) {
        return scanCodeRecordDao.searchHospitalRecord(scanCodeRecordBSearcher);
    }

    @Override
    public Integer countByTypeAndBizName(String type, String bizName) {
        return scanCodeRecordDao.countByTypeAndBizName(type, bizName);
    }

    @Override
    public Integer groupCountByTypeAndBizName(String type, String bizName) {
        return scanCodeRecordDao.groupCountByTypeAndBizName(type, bizName);
    }

    @Override
    public Integer countByTypeAndBizNameAndBizTime(String type, String bizName, String bizDatetime) {
        return scanCodeRecordDao.countByTypeAndBizNameAndBizTime(type, bizName, bizDatetime);
    }

    @Override
    public Integer countHospital() {
        return scanCodeRecordDao.countHospital();
    }

    @Override
    public Integer countHospitalByBizTime(String month) {
        return scanCodeRecordDao.countHospitalByBizTime(month);
    }

    @Override
    public List<DataStatsCDO> groupHospital(String adminAreaCode) {
        int level = AdminAreaCodeUtils.getLevel(adminAreaCode);
        String areaLike = AdminAreaCodeUtils.getCutdAreaCode(adminAreaCode) + "%";
        int nextLevel = level + 1;
        int levelLength = AdminAreaCodeUtils.getLevelLength(nextLevel);
        return scanCodeRecordDao.groupHospital(areaLike, nextLevel, levelLength);
    }

}
